package Master.interfaces;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Master.*;

/**
 * Project:    metsysdirg
 * File Name: ClientGUI.java
 * Change History:
 *     ********************************
 *     1.0     5/14/2011    Hengfeng Li
 *         Create the skeleton file
 *     ********************************
 *
 */
public abstract class ClientGUI extends JFrame{
	//Reference variables
	private MasterEngine masterengine_reference=null;
	
	/*
	 * Set of actions to do when being displayed for first time
	 */
	public void startup(MasterEngine masterengine_reference)
	{
		this.masterengine_reference=masterengine_reference;
		this.startup();
	}
	
	public void startup()
	{
		this.initialiseWindowGUI();
		this.initialise_listeners();
		this.display(getContentPane());
		
		getContentPane().validate();
		getContentPane().repaint();
	}
	
	/*
	 * Load its various components into window frame
	 * Input: Reference of window frame container to load components
	 * Output: Nothing
	 */
	protected abstract void display(Container screen_container);
	
	/*
	 * Set up listeners for human interactions for this screen
	 * Input: Reference of window frame in order to access to various methods
	 * Output: Nothing
	 */
	protected abstract void initialise_listeners();	
	
	/*
	 * Initialize the window frame
	 */
	public void initialiseWindowGUI()
	{
		try {
			setIconImage(ImageIO.read(new File(Configurations.app_icon)));
		} catch (Exception e) {
			System.out.println("ERROR: '"+Configurations.app_icon+"' is not readable!");
		}
		setTitle(Configurations.title);
		setSize(Configurations.screenWidth,Configurations.screenHeight);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent winEvt) {perform_exit_operation();}
		});	    
		
		//Center frame
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width/2)-(Configurations.screenWidth/2),
					(screenSize.height/2)-(Configurations.screenHeight/2));
	}
	
	/*
	 * Do actions prior to program exit
	 * frame.addWindowListener() will invoke this method when human clicks big X button or game exit button
	 */
	protected void perform_exit_operation()
	{
		int choice=JOptionPane.showConfirmDialog(this,"Exit the job management system?","Exit",
							JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
		if(choice==JOptionPane.NO_OPTION)
			return;
	
		System.exit(0);
	}
	
	protected MasterEngine masterEngineRef()
	{
		return masterengine_reference;
	}
}
